import { _decorator, Component, Label, Node, Size, Sprite, UITransform } from 'cc';
import { AppControll, EventName, PopView, skin_config } from '../manager/AppControll';
import EvtManger from '../tools/EvtManger';
import { PopManager } from '../manager/PopManager';
import { SystemMessage } from './SystemMessage';
import { Loader } from '../tools/LoadManger';
import { GameData } from '../system/GameData';
import { SoundManager } from '../manager/SoundManger';
const { ccclass, property } = _decorator;

@ccclass('GetSkinDebris')
export class GetSkinDebris extends Component {
    private config: skin_config = null;
    private mNdBody: Node = null;
    private mLbName: Label = null;
    private mIsLock: boolean = false;
    private mSprite: Sprite = null;
    private mLbEffectDescription: Label = null;
    private mCount = 1;
    private mCountDown = 10;
    private mLbCountDown: Label = null;
    private mIsHaveNoOperate = false;
    protected onLoad(): void {
        this.mNdBody = this.node.getChildByName("body");
        this.mLbName = this.mNdBody.getChildByName("Label").getComponent(Label);
        this.mSprite = this.mNdBody.getChildByName("debris").getChildByName("mainGun_silver").getComponent(Sprite);
        this.mLbEffectDescription = this.mNdBody.getChildByName("attr").getChildByName("attr").getComponent(Label);
        this.mLbCountDown = this.mNdBody.getChildByName("countDown").getComponent(Label);
        GameData.isPause = true;
        SoundManager.Instance<SoundManager>().PlayEffect("ui/getDebris");

    }

    InitSkinDebris(config: skin_config, count?) {
        this.config = config;
        if (count) this.mCount = count;
        this.InitSkinDebrisView();
    }

    InitSkinDebrisView() {
        this.mLbName.string = this.config.skinName + "";
        this.mNdBody.getChildByName("count").getComponent(Label).string = "x" + this.mCount.toString();
        Loader.LoadGunImage("mainGunSkin/" + this.config.mainGunSkinRes, (spf) => {
            this.mSprite.spriteFrame = spf;
        });
        this.mLbEffectDescription.string = this.config.unlockValue;

    }

    OnBtnConfirm() {
        this.mIsHaveNoOperate = true;
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick)
        if (this.mIsLock) return;
        EvtManger.do(EventName.dropSkin, this.config, 1);
        GameData.isPause = false;
        this.node.active = false;
    }

    OnBtnTriple() {
        this.mIsHaveNoOperate = true;
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick)
        if (this.mIsLock) return;
        this.mIsLock = true;
        this.scheduleOnce(() => {
            this.mIsLock = false
        }, 1);
        AppControll.Platform.ShowRewardVideoAd(this, (bool: boolean, str: string) => {
            this.mIsLock = false;
            if (!bool) {
                PopManager.Instance<PopManager>().ShowPop(PopView.systemMessage, (node) => {
                    node.getComponent(SystemMessage).InitContent(str);
                });
            }
            AppControll.Platform.SendBuryPoint(100010);
            EvtManger.do(EventName.dropSkinTriple, this.config, 1);
            this.node.active = false;
            GameData.isPause = false;
        })
    }

    protected update(dt: number): void {
        if (!this.mIsHaveNoOperate) {
            this.mCountDown -= dt;
            this.mLbCountDown.string = Math.floor(this.mCountDown) + "";
            if (this.mCountDown <= 0) {
                this.mIsLock = false;
                this.OnBtnConfirm();
            }
        }

    }
}


